package com.example.demo

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.example.demo.databinding.ActivityMainBinding
import com.wfy.orchestra.dispatch.DefaultTaskGraph
import com.wfy.orchestra.dispatch.EventTaskGraph
import com.wfy.orchestra.dispatch.ITask
import com.wfy.orchestra.dispatch.OnErrorListener
import com.wfy.orchestra.thread.CoroutineTaskExecutor
import com.wfy.orchestra.thread.MainThreadExecutor

/**
 * @author : wangfengyuan
 * e-mail  : 446950883@qq.com
 * time    : 2025/3/19
 * desc    :
 * version : 1.0
 */
class MainActivity : AppCompatActivity() {
    private val binding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_main)
        setContentView(binding.root)

        val taskGraph = EventTaskGraph(CoroutineTaskExecutor(lifecycleScope), lifecycleScope)
//        val taskGraph = DefaultTaskGraph(CoroutineTaskExecutor(lifecycleScope), MainThreadExecutor())
//        taskGraph.bindToLifecycle(lifecycle)

        // 定义任务
        val task0: ITask = object : ITask {

            override val id: String = "0"
            override var priority: Int = 0
            override fun execute() {
                LogUtils.logV("ITask 0 execute")
                LogUtils.logV("ITask 0 end")
            }
        }

        val task1: ITask = object : ITask {

            override val id: String get() = "1"
            override var priority: Int = 0

            override fun execute() {
                LogUtils.logV("ITask 1 execute")
                LogUtils.logV("ITask 1 end")
            }
        }
        val task2: ITask = object : ITask {
            override val id: String get() = "2"
            override var priority: Int = 0
            override fun execute() {
                LogUtils.logV("ITask 2 execute")
                LogUtils.logV("ITask 2 end")
            }
        }

        val task3: ITask = object : ITask {
            override val id: String get() = "3"
            override var priority: Int = 0
            override fun execute() {
                LogUtils.logV("ITask 3 execute")
                Thread.sleep(10 * 1000L)
                LogUtils.logV("ITask 3 end")
            }
        }
        val task4: ITask = object : ITask {
            override val id: String get() = "4"
            override var priority: Int = 0
            override fun execute() {
                LogUtils.logV("ITask 4 execute")
                Thread.sleep(20 * 1000L)
                LogUtils.logV("ITask 4 end")
            }
        }
        val task5: ITask = object : ITask {
            override val id: String get() = "5"
            override var priority: Int = 0
            override fun execute() {
                LogUtils.logV("ITask 5 execute")
                LogUtils.logV("ITask 5 end")
            }
        }
        // 添加任务和依赖
        taskGraph.addTask(task0, task1, task2, task3, task4, task5)
//        taskGraph.addTask(task1)
//        taskGraph.addTask(task2)
//        taskGraph.addTask(task3)
//        taskGraph.addTask(task4)
//        taskGraph.addTask(task5)
        // 添加依赖 方式一
//        taskGraph.addDependency("5", "3")
//        taskGraph.addDependency("5", "4")
//
//        taskGraph.addDependency("3", "0")
//        taskGraph.addDependency("3", "1")
//
//        taskGraph.addDependency("4", "1")
//        taskGraph.addDependency("4", "2")
//
//        // 添加依赖 方式二 推荐
//        taskGraph.addDependency("5", listOf("3", "4"))
//        taskGraph.addDependency("3", listOf("0", "1"))
//        taskGraph.addDependency("4", listOf("1", "2"))

        // 添加依赖 方式三 推荐
        taskGraph.configureDependencies {
            "5" dependsOn listOf("3", "4")
            "3" dependsOn listOf("0", "1")
            "4" dependsOn listOf("1", "2")
        }

        // 错误监听
        taskGraph.setOnErrorListener(object : OnErrorListener {
            override fun onError(task: ITask, cause: Throwable) {
                LogUtils.logV("TaskGraph ITask failed:id = ${task.id} cause = $cause")
            }
        })
        // 启动执行
        LogUtils.logV("generateDotGraph = ${taskGraph.generateDotGraph()}")
        taskGraph.start()
    }

    override fun onBackPressed() {
        super.onBackPressed()
        finish()
    }
}